﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace GameBanChim_Version1
{
    public class Bird : Character
    {
        private List<int> _listSpeedLevel;
        // bird state: 0: ready, 1: flying, 2:dead
        int _birdState = 0;

        public int BirdState
        {
            get { return _birdState; }
            set { _birdState = value; }
        }

        private int _interval;

        public int Interval
        {
            get { return _interval; }
            set { _interval = value; }
        }

        private int _iCurrentLevel;

        public int ICurrentLevel
        {
            get { return _iCurrentLevel; }
            set { _iCurrentLevel = value; }
        }

        private int _mark;

        public int Mark
        {
            get { return _mark; }
            set { _mark = value; }
        }
        public bool _flySlowly = false;
        public bool _notFlying = false;

        SoundEffect _ow;

        public override bool Init(ContentManager Content, int index, string strResourceXML)
        {
            XmlDocument _doc = new XmlDocument();
            _doc.Load(strResourceXML);

            XmlNode _bird = _doc.SelectSingleNode("//Bird[@id = '" + index.ToString() + "']");
            
            Nsprite = 2;
            Sprite = new MySprite[Nsprite];
            XmlNode _intervalNode = _bird.SelectSingleNode("Interval");
            Interval = int.Parse(_intervalNode.InnerText);

            XmlNode _flyImage = _bird.SelectSingleNode("FlyImage");

            int _frameNumber = int.Parse(_flyImage.SelectSingleNode("FrameNumber").InnerText);
            Texture2D[] _temp = new Texture2D[_frameNumber];
            for (int i = 0; i < _frameNumber; i++)
            {
                string _insert = _flyImage.SelectSingleNode("ImagePath").InnerText + "/" + i.ToString("00");
                _temp[i] = Content.Load<Texture2D>(_insert);
            }
            Sprite[0] = new MySprite(_temp, 0, 0, Interval);

            XmlNode _dieImage = _bird.SelectSingleNode("DieImage");
            _frameNumber = int.Parse(_dieImage.SelectSingleNode("FrameNumber").InnerText);
            _temp = new Texture2D[_frameNumber];
            for (int i = 0; i < _frameNumber; i++)
            {
                string _insert = _dieImage.SelectSingleNode("ImagePath").InnerText + "/" + i.ToString("00");
                _temp[i] = Content.Load<Texture2D>(_insert);
            }
            int _iIntervalDie = Interval + 20;
            Sprite[1] = new MySprite(_temp, 0, 0, _iIntervalDie);

            Rec = new Rectangle(0, 0, int.Parse(_bird.SelectSingleNode("Width").InnerText), int.Parse(_bird.SelectSingleNode("Height").InnerText));
            X = 800;
            Y = 0;

            _mark = int.Parse(_bird.SelectSingleNode("Mark").InnerText);

            XmlNodeList _listOfSpeed = _bird.SelectNodes("Speed");
            _listSpeedLevel = new List<int>();
            for (int i = 0; i < _listOfSpeed.Count; i++)
            {
                _listSpeedLevel.Add(int.Parse(_listOfSpeed[i].InnerText));
            }
            _ow = Content.Load<SoundEffect>("Sound/Ow");

            return true;
        }

        public override VisibleGameObject Clone()
        {
            Bird _new = new Bird();
            _new.Nsprite = Nsprite;
            _new.Sprite = new MySprite[_new.Nsprite];
            for (int i = 0; i < _new.Nsprite; i++)
            {
                _new.Sprite[i] = new MySprite(Sprite[i].Texture2D, X, Y, Interval);
            }
            _new.Isprite = 0;
            _new._ow = _ow;
            _new.Interval = Interval;
            _new.Rec = Rec;
            _new._mark = _mark;
            _new._listSpeedLevel = _listSpeedLevel;
            return _new;
        }

        public override void Update(GameTime gameTime)
        {
            if (_birdState == 1)
            {
                // nếu bị bắn: bắt sự kiện click trên chim:
                if (GlobalVariables.CurMouseState.LeftButton == ButtonState.Pressed && Rec.Contains(GlobalVariables.CurMouseState.X, GlobalVariables.CurMouseState.Y))
                {
                    Texture2D _text2D = Sprite[Isprite].Texture2D[Sprite[Isprite].Itexture2D];
                    Color[] _color = new Color[1];
                    int _XLocationOnTextture = (int)(GlobalVariables.CurMouseState.X - X);
                    int _YLocationOnTexture = (int)(GlobalVariables.CurMouseState.Y - Y);
                    _text2D.GetData<Color>(0, new Rectangle(_XLocationOnTextture, _YLocationOnTexture, 1, 1), _color, 0, 1);

                    if (_color[0].R != 0 && _color[0].G != 0 && _color[0].B != 0)
                    {
                        GlobalVariables.GamePlayer.Mark += _mark; //ghi điểm cho người chơi
                        if (_mark == 5)
                            GlobalVariables.GamePlayer.LargeBirdCount++;
                        else
                            GlobalVariables.GamePlayer.NormalBirdCount++;
                        _birdState = 2;
                        Isprite = 1;
                        _ow.Play();
                    }

                }
                base.Update(gameTime);
                // toc do bay cua con chim khi co skill tac dong va khi ko co skill tac dong
                if (_flySlowly)
                {
                    X -= (int)(_listSpeedLevel[_iCurrentLevel] * 2 / 10);
                    _flySlowly = false;
                }
                else if (_notFlying)
                {
                    X -= (int)(_listSpeedLevel[_iCurrentLevel] * 0);
                    _notFlying = false;
                } 
                else
                {
                    X -= _listSpeedLevel[_iCurrentLevel];
                }
            }
            else if (_birdState == 2)
            {
                if (Sprite[Isprite].Itexture2D != Sprite[Isprite].Ntexture2D - 1)
                    Sprite[Isprite].Itexture2D = (Sprite[Isprite].Itexture2D + 1) % Sprite[Isprite].Ntexture2D;
                Y += 5;
            }

        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Sprite[Isprite].Texture2D[Sprite[Isprite].Itexture2D], new Vector2(X, Y), Color.White);
            //base.Draw(gameTime, spriteBatch);
        }

        public bool IsVisible()
        {
            Point _point1 = new Point((int)X, (int)Y);
            Point _point2 = new Point((int)X + Rec.Width, (int)Y);
            Point _point3 = new Point((int)X, (int)Y + Rec.Height);
            Point _point4 = new Point((int)X + Rec.Width, (int)Y + Rec.Height);

            Rectangle _screenRec = new Rectangle(0, 0, 800, 600);
            if (_screenRec.Contains(_point1) || _screenRec.Contains(_point2) || _screenRec.Contains(_point3) || _screenRec.Contains(_point4))
                return true;
            return false;
        }
    }
}
